package com.stack.array;

public class ArrayStack {
    private int maxSize;
    private int top;
    private int[] array;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.array = new int[maxSize];
        this.top = -1;
    }

    // 判断栈是否满
    private boolean isFull() {
        return top == maxSize - 1;
    }
    // 入栈
    public void push(int data) {
        if (isFull()) {
            throw new RuntimeException("栈满，无法将数据入栈");
        }
        this.top++;
        array[top] = data;
    }

    // 判断栈是否为空
    private boolean isEmpty() {
        return top == -1;
    }
    // 出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无法出栈");
        }
        top--;
        return array[top + 1];
    }

    // 遍历栈
    public void show() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无法出栈");
        }
        // 从栈顶开始遍历
        for (int i = top; i >= 0; i--) {
            System.out.printf("%d ", array[i]);
        }

    }
}
